<template>
    <view class="editpaypwd">
       <view class="minela macl">
            <input type="text" v-model="verifyCode" placeholder="请您输入验证码" />
            <text style="border-left: 1px solid #ADB0BB;padding: 0 0 0 30px;color: #7F6AC3;"
                @tap="getPhonecode">{{codeBtn.codeText}}</text>
        </view>
        <view class="minela macl">
            <text>新的密码</text>
            <input type="password" v-model="password" placeholder="请您输入密码" />
        </view>
        <view class="minela macl">
            <text>确认密码</text>
            <input type="password" v-model="repasd" placeholder="请您再次输入新的密码" />
        </view>
        <view class="resetbtn" @click="logineditFn">
            {{psd}}
        </view>
    </view>
</template>

<script>
    let uinfo;
    import request from "../../untils/request.js";
    export default {
        data() {
            return {
                psd:'重置密码',
                opinSign: true,
                verifyCode: '',
                password: '',
                repasd: '',
                rules: {
                    password: {
                        rule: /(?![0-9A-Z]+$)(?![0-9a-z]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,20}$/,
                        msg: "请输入登录密码或密码格式错误",
                    },
                    verifyCode: {
                        rule: /^[0-9]/,
                        msg: "请输入验证码或格式错误",
                    },
                },
                //验证码按钮
                codeBtn: {
                    codeTime: 60,
                    codeText: "获取验证码",
                    codeStatus: true,
                },
            }
        },
        onLoad(options) {
            uinfo = uni.getStorageSync('userInfo');
            if(options.ab=='1'){
                this.psd='设置密码';
                uni.setNavigationBarTitle({
                　　title:'设置支付密码'
                })
            }else{
                this.psd='重置密码';
                uni.setNavigationBarTitle({
                　　title:'修改支付密码'
                })
            }
        },
        methods: {
            //修改支付密码
            logineditFn() {
                if (this.password != this.repasd) {
                    return uni.showToast({
                        icon: "none",
                        title: '两次密码不一致',
                    });
                }
                let telEmail = '0';
                if (uinfo.userLoginInfo.email == null) {
                    telEmail = '0';
                } else {
                    telEmail = '1';
                }
                uni.showLoading({
                    mask:true
                });
                request.postLoginris('/user/info/setTransPwd', {
                    accountNumber: uinfo.userLoginInfo.mobile, //手机号 ,
                    accountType: telEmail,
                    businessType: '3',
                    password: this.password,
                    verifyCode: this.verifyCode, //验证码
                    passwordType: '1',
                    userId: uinfo.userId
                }).then(res => {
                    console.log(res)
                    if (res.code == '200') {
                        uni.hideLoading();
                        uni.showToast({
                            icon: "none",
                            title: res.message,
                        });
                        setTimeout(function() {
                            uni.switchTab({
                                url: '/pages/mine/mine',
                            });
                        }, 1000)
                    } else {
                        uni.hideLoading();
                        uni.showToast({
                            icon: "none",
                            title: res.message,
                        });
                    }
                })
            },
            //获取验证码
            getPhonecode() {
                //0手机号，1邮箱
                let telEmail = '0';
				let accountNumber;
                if (uinfo.userLoginInfo.email == null) {
                    telEmail = '0';
					accountNumber = uinfo.userLoginInfo.mobile;
                } else {
                    telEmail = '1';
					accountNumber = uinfo.userLoginInfo.email;
                }
                if (this.codeBtn.codeStatus) {
                    this.codeBtn.codeStatus = false;
                    let timerId = setInterval(() => {
                        let codetime = this.codeBtn.codeTime;
                        codetime--;
                        this.codeBtn.codeTime = codetime;
                        this.codeBtn.codeText = "剩余" + codetime + "秒";
                        if (codetime < 1) {
                            clearInterval(timerId);
                            this.codeBtn.codeText = "重新获取";
                            this.codeBtn.codeTime = 60;
                            this.codeBtn.codeStatus = true;
                        }
                    }, 1000);
                    request.postMember('/user/login/sendVerifiyCode', {
                        accountNumber: accountNumber,
                        businessType: '3',
                        accountType: telEmail
                    }).then(res => {
                        console.log(res)
                        if (res.code == "200") {
                            
                            uni.showToast({
                                icon: "none",
                                title: '发送成功',
                            });
                        } else {
                            uni.showToast({
                                icon: "none",
                                title: res.message,
                            });
                        }
                    })
                }
            },
            validate(key) {
                let bool = true;
                if (!this.rules[key].rule.test(this[key])) {
                    //提示信息
                    uni.showToast({
                        icon: "none",
                        title: this.rules[key].msg,
                    });
                    //取反
                    bool = false;
                    return false;
                }
                return bool;
            },
        }
    }
</script>

<style>
    .editpaypwd {
        background-color: #F5F5F5;
        height: 90vh;
        padding: 46rpx 0 0 0;
    }

    .minela {
        width: 680rpx;
        background: #FFFFFF;
        height: 108rpx;
        margin: 0 auto 20rpx;
        border-radius: 6px;
        padding: 0 10px;
        display: flex;
        align-items: center;
    }

    .macl {
        justify-content: space-between;
    }

    .minela input {
        width: 400rpx;
        font-size: 14px;
        font-family: Source Han Sans CN;
        font-weight: 400;
    }

    .minela text {
        font-size: 14px;
        font-family: Source Han Sans CN;
        font-weight: 400;
        color: #8D8D8D;
    }

    .resetbtn {
        font-size: 14px;
        font-family: Source Han Sans CN;
        font-weight: 400;
        line-height: 38px;
        color: #FFFFFF;
        width: 355px;
        height: 38px;
        background: #7F6AC3;
        border-radius: 19px;
        text-align: center;
        margin: 100px auto 0;
    }
</style>
